package edu.csumb.itcd.teammario;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;

public class Bullet {

    protected int gwidth, gheight;
    protected int speed;
    protected Point bulletCenterPosition = new Point(0,0);
    protected Point targetCenterPosition = new Point(0,0);
    protected Point2D bulletPosition;
    protected int bulletDiameter = 0;
    protected int direction;

    protected boolean isActive = false;
    protected int damage;
    protected int range;
    protected double distY;
    protected double distX;
    protected double vectorY;
    protected double vectorX;
    protected double moveY;
    protected double moveX;
    
    public Bullet(Point2D start, Point2D destination, int damage, int range){
        
        if (isActive != true){
            

            this.damage = damage;
            this.range = range;
            speed = 11;
            bulletDiameter = 10;
            
            bulletPosition = new Point2D.Double(start.getX(), start.getY());
            
            vectorY = destination.getY() - (bulletDiameter/2) - start.getY();
            vectorX = destination.getX() - (bulletDiameter/2) - start.getX();

            //The dist variables need to be positive for the trig equations.
            distY = Math.abs(vectorY);
            distX = Math.abs(vectorX);

            //Gives the vectors a value of 1 or -1.
            vectorY /= distY;
            vectorX /= distX;

            /**
             * tan(x) = opposite/adjacent
             * tan(x) = distY/distX
             * x = arctan(distY/distX)
             *
             * cos(x) = adjacent/hypotenuse
             * cos(x) = moveX/speed
             * moveX = speed * cos(x)
             * moveX = speed * cos(arctan(distY/distX))
             */
            if (distX == 0 && distY == 0)
            {
                moveY = 0;
                moveX = 0;
            } else if (distX == 0)
            {
            	moveY = speed * vectorY;
            	moveX = 0;
            } else if (distY == 0)
            {
            	moveY = 0;
            	moveX = speed * vectorX;
            } else
            {
                moveY = speed * Math.sin(Math.atan(distY/distX));
                moveX = speed * Math.cos(Math.atan(distY/distX));
//                moveY = Math.rint(moveY) * vectorY;
//                moveX = Math.rint(moveX) * vectorX;
                moveY = moveY * vectorY;
                moveX = moveX * vectorX;
            }

            
//            slopeX = destination.x - start.x;
//            if (slopeX < 0){ slopeX *= -1;}
//            if (start.x > destination.x){slopeX *= -1;}
//
//            slopeY = destination.y - start.y;
//            if (slopeY < 0){ slopeY *= -1;}
//            if (start.y > destination.y){slopeY *= -1;}
//            
//            this.damage = damage;
//            this.range = range;
            
            isActive = true;
        }
    }
    public int getDamage() {
        return damage;
    }
    public void setDamage(int damage) {
        this.damage = damage;
    }
    public void movebullet(){
        
        if (isActive == true)
        {
            bulletPosition.setLocation(bulletPosition.getX() + moveX, bulletPosition.getY() + moveY);
            
            if((bulletPosition.getX()>800)||(bulletPosition.getX()<-50)||(bulletPosition.getY()>600)||(bulletPosition.getY()<50))
            {    
                isActive = false;
            }

        }
    }
	 public Point getBulletCenterPosition()
	 {
	     bulletCenterPosition.x = (int) (bulletPosition.getX() + (bulletDiameter/2));
	     bulletCenterPosition.y = (int) (bulletPosition.getY() + (bulletDiameter/2));
	     return bulletCenterPosition;
	 }
	 
	 public Point2D getBulletPosition()
	 {
	     return bulletPosition;
	 }
	 
	 public boolean bulletIsActive()
	 {
	     return isActive;
	 }
	 public int getBulletDiameter()
	 {
	     return bulletDiameter;
	 }
	 
	 public void setIsActive(boolean isActive)
	 {
	     this.isActive = isActive;
	 }
	 
	 public void paintBullet(Graphics2D g2)
	 {
		 if (isActive == true)
	     {
			 Ellipse2D bulletImage = new Ellipse2D.Double(bulletPosition.getX(), bulletPosition.getY(), bulletDiameter, bulletDiameter);
			 g2.fill(bulletImage);
	     }
	 }
	 
	 public Ellipse2D getBulletImage()
	 {
		 return new Ellipse2D.Double(bulletPosition.getX(), bulletPosition.getY(), (double) bulletDiameter,(double) bulletDiameter);
	 }
	public void enemyOnHit(Enemy enemy)
	{

	}
	
	public void playerOnHit()
	{

	}

}